package com.empire.module.member.controller.admin.mappolygon;

import com.empire.module.member.controller.admin.mappolygonpoint.vo.PointInPolygonReqVO;
import com.empire.module.member.controller.admin.mappolygonpoint.vo.PointInPolygonRespVO;
import com.empire.module.member.service.mappointInarea.PointInPolygonService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.empire.framework.common.pojo.PageParam;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.pojo.CommonResult;
import com.empire.framework.common.util.object.BeanUtils;
import static com.empire.framework.common.pojo.CommonResult.success;

import com.empire.framework.excel.core.util.ExcelUtils;

import com.empire.framework.apilog.core.annotation.ApiAccessLog;
import static com.empire.framework.apilog.core.enums.OperateTypeEnum.*;

import com.empire.module.member.controller.admin.mappolygon.vo.*;
import com.empire.module.member.dal.dataobject.mappolygon.MapPolygonDO;
import com.empire.module.member.service.mappolygon.MapPolygonService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 地图多边形信息")
@RestController
@RequestMapping("/member/map-polygon")
@Validated
public class MapPolygonController {

    @Resource
    private MapPolygonService mapPolygonService;

    @Resource
    private PointInPolygonService pointInPolygonService;

    @PostMapping("/judge")
    @Operation(summary = "判断点是否在多边形内")
    @PreAuthorize("@ss.hasPermission('member:map-polygon:query')")
    public CommonResult<PointInPolygonRespVO> judgePointInPolygon(
            @Valid @RequestBody PointInPolygonReqVO reqVO) {
        return CommonResult.success(pointInPolygonService.judgePointInPolygon(reqVO));
    }

    @PostMapping("/create")
    @Operation(summary = "创建地图多边形信息")
    @PreAuthorize("@ss.hasPermission('member:map-polygon:create')")
    public CommonResult<String> createMapPolygon(@Valid @RequestBody MapPolygonSaveReqVO createReqVO) {
        return success(mapPolygonService.createMapPolygon(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新地图多边形信息")
    @PreAuthorize("@ss.hasPermission('member:map-polygon:update')")
    public CommonResult<Boolean> updateMapPolygon(@Valid @RequestBody MapPolygonSaveReqVO updateReqVO) {
        mapPolygonService.updateMapPolygon(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除地图多边形信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:map-polygon:delete')")
    public CommonResult<Boolean> deleteMapPolygon(@RequestParam("id") String id) {
        mapPolygonService.deleteMapPolygon(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得地图多边形信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:map-polygon:query')")
    public CommonResult<List<MapPolygonWithPointsVO>> getMapPolygon(@RequestParam("id") String id) {
        List<MapPolygonWithPointsVO> mapPolygon = mapPolygonService.getPolygonsByConfigId(id);
        return success(mapPolygon);
    }

    @GetMapping("/page")
    @Operation(summary = "获得地图多边形信息分页")
    @PreAuthorize("@ss.hasPermission('member:map-polygon:query')")
    public CommonResult<PageResult<MapPolygonRespVO>> getMapPolygonPage(@Valid MapPolygonPageReqVO pageReqVO) {
        PageResult<MapPolygonDO> pageResult = mapPolygonService.getMapPolygonPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MapPolygonRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出地图多边形信息 Excel")
    @PreAuthorize("@ss.hasPermission('member:map-polygon:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMapPolygonExcel(@Valid MapPolygonPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MapPolygonDO> list = mapPolygonService.getMapPolygonPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "地图多边形信息.xls", "数据", MapPolygonRespVO.class,
                        BeanUtils.toBean(list, MapPolygonRespVO.class));
    }

}